package spring1;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@Component
public class FileWriterService {
	private File f;
	private FileWriter w;
	
	@Value("${path}")
	private String path;

	@PostConstruct	
	public void init(){
		f = new File(path);
		try {
			f.createNewFile();
		} catch (IOException e) {			
			e.printStackTrace();
		}		
	}
	
	
	@PreDestroy	
	public void cleanup(){
		try {
			w.close();
		} catch (IOException e) {			
			e.printStackTrace();
		}
	}
	
	public void write(String content){
		try {
			w = new FileWriter(f);
			w.write(content);
		} catch (IOException e) {			
			e.printStackTrace();
		}
		
	}
}
